package
{
	import com.ling.brushes.BrushControl;
	import com.ling.denfind.EditorDfined;
	import com.ling.events.CreateLevelEvent;
	import com.ling.levelCon.LevelListControl;
	import com.ling.map.GridMap;
	import com.ling.ui.PrevieXMLPanel;
	import com.ling.uiLogic.MapCategoryControl;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import mx.controls.Alert;
	import mx.events.MenuEvent;
	import mx.managers.PopUpManager;

	public class MainManager
	{
		private static var _app:LingEditor;
		
		/**
		 * init once,reset when a new map is created
		 */		
		private static var mapCategoryCon:MapCategoryControl;
		private static var _brushCon:BrushControl;
		private static var levelListCon:LevelListControl;
		
		private static var map:GridMap;
		//
		private static var previewPanel:PrevieXMLPanel;
		
		public function MainManager()
		{
		}
		public static function setup(app:LingEditor):void
		{
			_app = app;
			/*var map:GridMap = new GridMap();
			_app.mainStage.addElement(map);*/
			//
			mapCategoryCon = new MapCategoryControl(app);
			mapCategoryCon.enabled = false;
			_brushCon = new BrushControl(app);
			levelListCon = new LevelListControl(app);
			levelListCon.addEventListener(CreateLevelEvent.CREATE_NEW_LEVEL,onCreateLevel);
			//
			app.menuBar.addEventListener(MenuEvent.ITEM_CLICK,onClickMenu);
			app.saveBtn.addEventListener(MouseEvent.CLICK,onSave);
			//
			app.importBtn.addEventListener(MouseEvent.CLICK,previewHandler);
		}
		
		private static function previewHandler(event:MouseEvent):void
		{
			if(!previewPanel)
			{
				previewPanel = new PrevieXMLPanel();
			}
			previewPanel.text = export(false);
			PopUpManager.addPopUp(previewPanel,_app,true);
			PopUpManager.centerPopUp(previewPanel);
		}
		
		private static function onSave(event:MouseEvent):void
		{
			export();
		}
		
		private static function onClickMenu(event:MenuEvent):void
		{
			var x:XML = event.item as XML;
			var str:String = x.@data;
			MainManager[str]();
		}
		
		/**
		 * 导出XML表
		 * 
		 */		
		private static function export(isSave:Boolean = true):String
		{
			var xml:XML = <root x_n={EditorDfined.W_NUM} y_n={EditorDfined.H_NUM} level={map.level}/>;
			xml.appendChild(mapCategoryCon.getXML());
			xml.appendChild(map.getXML());
			
			//trace(xml.toXMLString());
			if(isSave)
			{
				var file:File = new File();
				file.addEventListener(Event.COMPLETE,onFileSave);
				file.save(xml,map.level+".xml");
			}
			return xml.toXMLString();
		}
		private static function onFileSave(event:Event):void
		{
			Alert.show("Save Complete","",Alert.OK);
		}
		
		
		public static function get brushCon():BrushControl
		{
			return _brushCon;
		}
		
		
		private static function onCreateLevel(event:CreateLevelEvent):void
		{
			if(map)
				map.destroy();
			//
			map = new GridMap(event.w,event.h);
			map.level = event.level;
			_app.mainStage.addElement(map);
			mapCategoryCon.enabled = true;
			_app.level_txt.text = map.level.toString();
		}
	}
}